﻿using Customer.Domain;
using Customer.Infrstructre.ClientManager;
using Customer.Infrstructre.CusContactManager;
using Customer.Read.API.Applcation.Command.ClientCommand;
using MediatR;
using System.Diagnostics.Contracts;
using Warehouse.Domain.DTO;

namespace Customer.Read.API.Applcation.CommandHandler.ClientCommanfHandler
{
    /// <summary>
    /// 客户反填
    /// </summary>
    public class ClientFanTCommandHandler:IRequestHandler<ClientFanTCommand, List<FanClientDTO>>
    {
        private readonly IClientRepository _clientRep;
        private readonly ICusContactRepository _cusContactRep;
        public ClientFanTCommandHandler(IClientRepository _clientRep, ICusContactRepository cusContactRep)
        {
            this._clientRep = _clientRep;
            this._cusContactRep = cusContactRep;
        }

        public async Task<List<FanClientDTO>> Handle(ClientFanTCommand request, CancellationToken cancellationToken)
        {
            var cus = await _cusContactRep.GetAllAsync();
            var list = from a in await _clientRep.GetAllAsync()
                       where a.ClientId==request.ClientId
                       select new FanClientDTO
                       {
                           ClientId = a.ClientId,
                           ClientCode = a.ClientCode,
                           ClientName = a.ClientName,
                           Cfax = a.Cfax,
                           Landline = a.Landline,
                           Mailbox = a.Mailbox,
                           CusContactId = a.CusContactId,
                           Cremark = a.Cremark,
                           Office_Id = a.Office_Id,
                           User_Id = a.User_Id,
                           cusContactslist = (from c in cus
                                              where c.CusContactId == a.CusContactId
                                              select new CusContact
                                              {
                                                  CusContactId=c.CusContactId,
                                                  Ccontact=c.Ccontact,
                                                  Caddress=c.Caddress,
                                                  Cphone=c.Cphone,
                                                  CreatTime=c.CreatTime,
                                                  Office_Id=c.Office_Id,
                                                  User_Id=c.User_Id
                                              }).ToList()
                       };
            return list.ToList();
            //return await _clientRep.GetModelAsync(request.ClientId);
        }
    }
}
